package com.huatai.datacenter.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.huatai.common.api.R;
import com.huatai.common.util.StringUtil;
import com.huatai.datacenter.entity.DataServicesEntity;
import com.huatai.datacenter.entity.RabbitMQEntity;
import com.huatai.datacenter.entity.TopicTaskEntity;
import com.huatai.datacenter.service.DataModelApplyService;
import com.huatai.datacenter.service.RabbitMQService;
import com.huatai.datacenter.service.TopicTaskService;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.xxl.job.core.handler.annotation.XxlJob;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeoutException;

/**
 * @author Lion
 * @date 2023/8/1  10:48
 */
@RequestMapping("/rabbit")
@RestController
@Api(value = "rabbitMQ管理",tags = "rabbitMQ管理")
@Log4j
public class RabbitMQController extends BaseController<RabbitMQService, RabbitMQEntity>{

	@Autowired
	private DataServiceController dataServiceController;

	@Autowired
	private DataModelApplyService dataModelApplyService;

	@Autowired
	private TopicTaskService taskService;

	@ApiOperation("新增或者更新rabbitmq")
	@Override
	@PostMapping("addOrUpdateRabbitMq")
	@ApiOperationSupport(includeParameters = {
		"rabbitMQEntity.id",
		"rabbitMQEntity.clusterName",
		"rabbitMQEntity.host",
		"rabbitMQEntity.port",
		"rabbitMQEntity.username",
		"rabbitMQEntity.password",
		"rabbitMQEntity.virtualHost",
		"rabbitMQEntity.managePort",
		"rabbitMQEntity.exchangeName"
	})
	public R save(@RequestBody RabbitMQEntity rabbitMQEntity){

		int status = this.service.getConnection(rabbitMQEntity);
		rabbitMQEntity.setStatus(status);
		this.service.saveOrUpdate(rabbitMQEntity);

		return R.success("更新成功");
	}

	/**
	 * 删除rabbitmq集群
	 * @param id
	 * @return
	 */
	@ApiOperation("删除rabbitmq集群")
	@PostMapping("/removeRabbitMqById")
	public R removeRabbitMqById(@RequestParam String id){
		this.service.removeById(id);
		return R.success("删除成功");
	}

	/**
	 * 获取rabbitmq集群清册
	 * @return
	 */
	@ApiOperation("获取rabbitmq集群")
	@PostMapping("/getRabbitMqList")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "page", value = "页数"),
		@ApiImplicitParam(name = "limit", value = "条数"),
		@ApiImplicitParam(name = "clusterName", value = "集群名称"),
	})
	public R getRabbitMqList(@RequestBody JSONObject jsonObject){
		Integer page = jsonObject.getInteger("page");
		Integer limit = jsonObject.getInteger("limit");
		if (page==0 && limit==0){
			List<RabbitMQEntity> list = this.service.list();
			return R.data(list);
		}
		//集群名称
		String clusterName = jsonObject.getString("clusterName");
		Page<RabbitMQEntity> page1 = new Page<>(page, limit);
		LambdaQueryWrapper<RabbitMQEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.like(StringUtil.isNotBlank(clusterName),RabbitMQEntity::getClusterName,clusterName);
		this.service.page(page1,wrapper);
		return R.data(page1);
	}


	/**
	 * 获取集群下路由和消息数量信息
	 */
	@PostMapping("getRoutingKeyInfo")
	@ApiOperation("获取集群下路由和消息数量信息")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "page", value = "页数"),
		@ApiImplicitParam(name = "limit", value = "条数"),
		@ApiImplicitParam(name = "clusterId", value = "集群id"),
	})
	public R getRoutingInfo(@RequestBody JSONObject jsonObject) throws Exception {
		return this.service.getRabbitmqRouting(jsonObject);
	}

	/**
	 * 添加rabbitMq任务并启动监听
	 * @param topicTaskEntity
	 * @return
	 */
	@PostMapping("createRabbitMqTask")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "路由任务id", dataType = "long"),
		@ApiImplicitParam(name = "topicName", value = "路由名称", dataType = "String"),
		@ApiImplicitParam(name = "topicTaskName", value = "路由任务名称", dataType = "String"),
		@ApiImplicitParam(name = "clusterId", value = "集群id", dataType = "long"),
		@ApiImplicitParam(name = "clustername", value = "集群名称", dataType = "String"),
		@ApiImplicitParam(name = "modelId", value = "模型id", dataType = "long"),
		@ApiImplicitParam(name = "modelName", value = "模型名称", dataType = "String"),
		@ApiImplicitParam(name = "comments", value = "任务描述", dataType = "String"),
		@ApiImplicitParam(name = "dataTable", value = "表名", dataType = "String"),
		@ApiImplicitParam(name = "qualityRule", value = "质量规则", dataType = "String"),
		@ApiImplicitParam(name = "typeMq", value = "消息类型", dataType = "String")
	})
	@ApiOperation("添加rabbitMq任务并启动监听")
	public R createRabbitMqTask(@RequestBody TopicTaskEntity topicTaskEntity) {
		return this.service.createRabbitMqTask(topicTaskEntity);
	}

	/**
	 * 定时任务，定时去连接rabbitmq中监听路由
	 */
	@XxlJob("timingConnectionRouting")
	public void timingConnectionRouting(){
		this.service.timingConnectionRouting();
	}

	/**
	 * 创建rabbitmq路由
	 */
	@PostMapping("/crateRouting")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "集群id", dataType = "long"),
		@ApiImplicitParam(name = "routingName", value = "路由名称", dataType = "String"),
	})
	@ApiOperation("创建rabbitmq路由")
	public R crateRouting(@RequestBody JSONObject jsonObject) throws IOException {
		return this.service.crateRouting(jsonObject);
	}

	/**
	 * 发送示例数据
	 * @param jsonObject
	 * @return
	 */
	@PostMapping("/sendExampleData")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "集群id", dataType = "long"),
		@ApiImplicitParam(name = "routingName", value = "路由名称", dataType = "String"),
		@ApiImplicitParam(name = "data", value = "示例数据", dataType = "JSONObject")
	})
	@ApiOperation("发送示例数据")
	public R sendExampleData(@RequestBody JSONObject jsonObject){
		return this.service.sendExampleData(jsonObject);  
	}


	/**
	 * 消费的示例数据
	 */
	@PostMapping("/getExampleData")
	@ApiOperation("消费的示例数据")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "集群id", dataType = "long"),
		@ApiImplicitParam(name = "routingName", value = "路由名称", dataType = "String"),
	})
	public R getExampleData(@RequestBody JSONObject jsonObject){
		return this.service.getExampleData(jsonObject);
	}




}
